﻿/**
 * IMonAIR: A new open-source library for instant messenger connectivity. <http://code.google.com/p/imonair/>
 *
 * This software is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
 */
/**
 * @author Andre de Groot <http://www.cmd.tech.nhl.nl/users/groot406/weblog/>
 * @author Piotr Zema <http://felixz.mark-naegeli.com>
 */
package {
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import net.imonair.core.contacts.IMContactList;
	import net.imonair.core.events.IMContactListEvent;
	import net.imonair.msn.connectors.MSNConnector;
	import net.imonair.msn.contacts.MSNContact;
	import net.imonair.msn.contacts.MSNStatus;
	import net.imonair.msn.events.MSNContactListEvent;
	import net.imonair.msn.events.MSNConnectorEvent;
	import net.imonair.msn.events.MSNContactEvent;
	import net.imonair.msn.events.MSNChatEvent;
	import net.imonair.msn.chats.MSNMessage;
	import fl.controls.*;
	import fl.data.DataProvider;
	public class IMonAIR extends Sprite {
		private const LIVE_ID:String = "imonair@hotmail.com";
		private const PASSWORD:String = "*&obelix";
		private var msn:MSNConnector;
		private var dp:DataProvider;			
		public function IMonAIR() {
			initStatusChanger()
			msn = new MSNConnector();
			//msn.addEventListener(MSNConnectorEvent.CONNECT, connected);
			msn.addEventListener(MSNConnectorEvent.SIGN_IN, loggedIn);
			msn.debug = true;
			//msn.addEventListener(MSNConnectorEvent.CONTACTS_READY, 				contactListSynched);
			msn.addEventListener(MSNContactEvent.ONLINE,				newContactOnline);
			msn.addEventListener(MSNContactEvent.ADDED_BY_CONTACT, 			newFriendInvitationHandler);
			//msn.addEventListener(MSNContactEvent.CONTACT_ADDED, 				newContactHandler);
			//msn.addEventListener(MSNContactEvent.CONTACT_REMOVED,				removedContactHandler);
			msn.addEventListener(MSNContactEvent.CHANGE, 				contactUpdateHandler);
			msn.addEventListener(MSNChatEvent.MESSAGE_RECEIVED, messageReceivedHandler);
			msn.addEventListener(MSNContactListEvent.SYNCED, contactListSynced);
			//msn.addEventListener(MSNConnectorEvent.GROUP_CONVERSATION_STARTED, 	groupSessionsChanged);
			//msn.addEventListener(MSNConnectorEvent.GROUP_CONVERSATION_CLOSED, 	groupSessionsChanged);
			//msn.addEventListener(MSNChatEvent.CONTACT_JOINED, 		groupSessionsChanged);
			//msn.addEventListener(MSNChatEvent.CONTACT_LEFT, 		groupSessionsChanged);

			removeContactBTN.addEventListener(	MouseEvent.CLICK, removeContactHandler);
			addBTN.addEventListener(			MouseEvent.CLICK, addContact);
			blockContactBTN.addEventListener(	MouseEvent.CLICK, blockContact);
			unBlockContactBTN.addEventListener(	MouseEvent.CLICK, unBlockContact);
			changeNameBTN.addEventListener( 	MouseEvent.CLICK, changeName);
			login_window.signin_btn.addEventListener(	MouseEvent.CLICK,loginClickHandler)
		}
		private function loginClickHandler(evt:MouseEvent) {
			//loginWindow.close();
			removeChild(login_window)
			msn.signIn(login_window.email_inp.text, login_window.password_inp.text);
		}
		private function initStatusChanger():void {
			statusChanger.addItem({label:"Online",		value:MSNStatus.ONLINE});
			statusChanger.addItem({label:"Away", 		value:MSNStatus.AWAY});
			statusChanger.addItem({label:"Busy", 		value:MSNStatus.BUSY});
			statusChanger.addItem({label:"Be Right Back", value:MSNStatus.BE_RIGHT_BACK});
			statusChanger.addItem({label:"Telephone", 	value:MSNStatus.IN_A_CALL});
			statusChanger.addItem({label:"Out to Lunch",value:MSNStatus.OUT_TO_LUNCH});
			statusChanger.addItem({label:"Idle", 		value:MSNStatus.IDLE});
			statusChanger.addItem({label:"Hidden", 		value:MSNStatus.HIDDEN});
			statusChanger.rowCount = statusChanger.length;
		}
		private function loggedIn(event:MSNConnectorEvent) {
			event.currentTarget.removeEventListener(MSNConnectorEvent.SIGN_IN, loggedIn);
			msn.addEventListener(IMContactListEvent.SYNCED, contactListSynced);
			statusChanger.addEventListener(Event.CHANGE, changeStatus);
			nameInput.text = event.target.displayName;
			dp = new DataProvider();
			contacts_dg.addColumn('displayName');
			contacts_dg.dataProvider = dp;
			msn.syncContactList();
		}
		private function contactListSynced(e:MSNContactListEvent) {
			msn.status = MSNStatus.AWAY;
			dp = new DataProvider();
			contacts_dg.dataProvider = dp;
			dp.addItems(msn.onlineContactList.contacts.sortOn('displayName'));
			//contacts_dg.addEventListener(MouseEvent.DOUBLE_CLICK, openChat);
		}
		private function newContactOnline(e:MSNContactEvent) {
			//this.dp.addItem(e.data)
		}
		private function newFriendInvitationHandler(e:MSNConnectorEvent) {
			//trace('got invitation');
		}
		private function newContactHandler(e:MSNContactEvent) {
			//trace('friend added to contactlist');
		}
		private function removeContactHandler(e:MouseEvent) {
			msn.removeContact(this.contacts_dg.selectedItem.email, false)
		}
		private function removedContactHandler(e:MSNContactEvent) {
			refreshContactList();
		}
		private function addContact(e:MouseEvent) {
			msn.addContact(contactInput.text);
		}
		private function blockContact(e:MouseEvent) {
			msn.blockContact(this.contacts_dg.selectedItem.email)
		}
		private function unBlockContact(e:MouseEvent) {
			msn.unBlockContact(this.contacts_dg.selectedItem.email)
		}
		private function contactUpdateHandler(e:MSNContactEvent) {
			refreshContactList();
		}
		private function changeStatus(e:Event) {
			msn.status=e.target.selectedItem.value;
		}
		private function changeName(e:MouseEvent) {
			msn.displayName=this.nameInput.text;
		}
		private function selectContact(msg:Object):void {
			for(var i:uint = 0; i < this.contacts_dg.length; i++) {
				if(msg.switchBoard == this.contacts_dg.getItemAt(i).switchBoard)
					this.contacts_dg.selectedIndex = i;
			}
		}
		private function messageReceivedHandler(e:MSNChatEvent):void {
			trace(e);
		}
		private function refreshContactList():void {
			if(this.contacts_dg.selectedItem)
				var oldSelectedEmail:String = this.contacts_dg.selectedItem.email;
			this.dp.removeAll();
			dp.addItems(msn.onlineContactList.contacts.sortOn('displayName'));
			
			//addGroupSessions();
			
			if(oldSelectedEmail)
				for(var i:uint = 0; i<this.dp.length; i++)
					if(this.dp.getItemAt(i).email == oldSelectedEmail)
						this.contacts_dg.selectedIndex = i
		}
	}
}
/*	import MSNChatWindow;
		
	public class MSNClient_AIR extends Sprite
	{
		private var msn		:MSNConnector;
		private var randInt	:uint
		private var score	:Object = new Object();
		
		private var confersations:Array = new Array();
		private var openChats:Array = new Array();
		
		private var loginWindow:NativeWindow;
		
		public function MSNClient_AIR()
		{
			createLoginWindow();
					
			msn = new MSNConnector();
			msn.addEventListener(Event.CONNECT, connected)
			
			initStatusChanger();
			configureListeners();
		}
		
		private function openChat(e:*)
		{		
			var chatWindow:MSNChatWindow;
			if(!this.openChats[this.contacts_dg.selectedItem.email])
			{
				chatWindow = new MSNChatWindow(msn, this.contacts_dg.selectedItem);
				this.openChats[this.contacts_dg.selectedItem.email] = chatWindow;
			} else
				chatWindow = this.openChats[this.contacts_dg.selectedItem.email];
						
			chatWindow.activate();
		}
		
		
		
		
		private function groupSessionsChanged(e:MSNEvent)
		{
			refreshContactList();
		}
		
		private function addGroupSessions()
		{
			for(var i:uint = 0; i < msn.getGroupSessions().length; i++)
			{
				var groupNames:Array = new Array();
				
				for(var j:uint = 0; j < msn.getGroupSessions()[i].contacts.length; j++)
					groupNames.push(msn.getGroupSessions()[i].contacts[j].nickname);
				
				var listObj:Object 	= new Object();
				listObj.nickname 	= groupNames.join(", ");
				listObj.switchBoard	= msn.getGroupSessions()[i];
				listObj.email		= 'group_'+[i];
				this.dp.addItem(listObj);
			}
		}
		
		
	}
}

*/